#!/usr/bin/python

import sys
import matplotlib.pyplot as plt
import pylab
import numpy as np

file1 = sys.argv[1]
f1 = open(file1, "rU")
fl1 = f1.readlines()

file2 = sys.argv[2]
f2 = open(file2, "rU")
fl2 = f2.readlines()

x1 = []
x2 = []
y1 = []
y2 = []

line = fl1[4]
l = line.split('\t')
genome_size = int(l[7])

f1_coords = {}

for i in range(1, genome_size+1):
    f1_coords[i] = 0

for i, line in enumerate(fl1[4:]):
    l = line.split('\t')
    start = int(l[0])
    stop = int(l[1])
    gs = int(l[7])
    identity = float(l[6])
    x1.append(start)
    x2.append(stop)
    y1.append(identity)
    y2.append(identity)
    for a in range(start, stop):
        f1_coords[a] = f1_coords[a] + 1

cov1 = []

for k, v in f1_coords.iteritems():
    cov1.append(v)

x3 = []
x4 = []
y3 = []
y4 = []

f2_coords = {}
for i in range(1, genome_size+1):
    f2_coords[i] = 0

for j, line in enumerate(fl2[4:]):
    l = line.split('\t')
    start = int(l[0])
    stop = int(l[1])
    gs = int(l[7])
    identity = float(l[6])
    x3.append(start)
    x4.append(stop)
    y3.append(identity)
    y4.append(identity)
    for a in range(start, stop):
        f2_coords[a] = f2_coords[a] + 1

cov2 = []

for k, v in f2_coords.iteritems():
    cov2.append(v)

gx = [1,genome_size]
gy = [10,10]

a1 = [x1, x2]
b1 = [y1, y2]

xcoords = range(1, genome_size+1)
#ycoords = cov

print "# of records in xcoords: ", len(xcoords)
print "# of records in cov1: ", len(cov1)



fig = plt.figure(1, figsize=(16,6))

#plt.subplot(211)
#plt.plot(xcoords, cov, color='#CC99CC', linestyle='-', alpha=0.4)
#plt.fill_between(xcoords, cov1, facecolor='#660066')
plt.fill_between(xcoords, cov1, facecolor='#660066', alpha=0.4)
plt.fill_between(xcoords, cov2, facecolor='#33FF33', alpha=0.4)
#plt.plot(xcoords, cov1, color='#660066', linestyle='-', alpha=0.4)
#plt.fill_between(xcoords, cov2, facecolor='#FFFF00', alpha=0.3)
#plt.annotate('test', xy=(40000, 40), xycoords='data', xytext=(30, 30), textcoords='offset points', arrowprops=dict(arrowstyle="->"))
plt.axis([0, genome_size, 0, max(cov1)+2])
plt.title('Coverage of reads against the genome based on MUMMER alignment')
plt.xlabel('Genome position (bp)')
plt.ylabel('Counts')
plt.grid(True)

#plt.subplot(212)
#plt.fill_between(xcoords, cov2, facecolor='#FFFF00')
#plt.fill_between(xcoords, cov2, facecolor='#99CC33', alpha=0.4)
#plt.plot(xcoords, cov2, color='#99CC33', linestyle='-', alpha=0.4)
#plt.axis([0, genome_size, 0, max(cov1)+2])
#plt.title('Coverage of reads against the genome based on MUMMER alignment')
#plt.xlabel('Genome position (bp)')
#plt.ylabel('Counts')
#plt.grid(True)

plt.show()

#plt.savefig(outfile, format='pdf')
